package com.ai3d.rfid.network.interceptor

import com.ai3d.rfid.common.proxy.MMKVProxy
import com.ai3d.rfid.common.proxy.MMKVProxy.KEY_SETTINGS_LANGUAGE_MAIN
import com.ai3d.rfid.common.proxy.MMKVProxy.KEY_TOKEN
import com.bj.ski.base.common.utils.DiscConstants.HEADER_CONTENT_LANGUAGE
import com.bj.ski.base.common.utils.DiscConstants.HEADER_CONTENT_LANGUAGE_EN_US
import com.bj.ski.base.common.utils.DiscConstants.HEADER_CONTENT_LANGUAGE_ZH_CN
import com.bj.ski.base.common.utils.DiscConstants.HEADER_CONTENT_TOKEN
import com.blankj.utilcode.util.AppUtils
import okhttp3.Interceptor
import okhttp3.Response
import java.io.IOException

/**
 * Customized Head interceptor
 */
class CommonHeadInterceptor : Interceptor {

    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        val builder = chain.request().newBuilder()
        var loginToken = MMKVProxy.getString(KEY_TOKEN)
        if (null == loginToken) {
            loginToken = ""
        }
        builder.addHeader(HEADER_CONTENT_TOKEN, loginToken).build()
        builder.addHeader("device", "Android").build()
        builder.addHeader("versionName", AppUtils.getAppVersionName()).build()
        builder.addHeader("versionCode", AppUtils.getAppVersionCode().toString()).build()
        var language =
            when (MMKVProxy.getString(KEY_SETTINGS_LANGUAGE_MAIN, HEADER_CONTENT_LANGUAGE_EN_US)) {
                HEADER_CONTENT_LANGUAGE_EN_US -> HEADER_CONTENT_LANGUAGE_EN_US
                else -> HEADER_CONTENT_LANGUAGE_ZH_CN
            }
        builder.addHeader(
            HEADER_CONTENT_LANGUAGE,
            language
        ).build()
        return chain.proceed(builder.build())
    }

}